package geppetto.main;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import java.util.LinkedList;
import java.util.Map;
import java.util.Properties;

public class GeppettoParameters {
	protected Properties _properties = new Properties();
	protected String[] _required = new String[] {};

	public GeppettoParameters() {

	}

	public void loadParametersFromMap(Map<String, String> map) {
		for (String key : map.keySet()) {
			_properties.put(key, map.get(key));
		}
	}

	public void loadParametersFromCommandLineArgs(String[] args) {
		for (int i = 0; i < args.length; i++) {
			if (args[i].startsWith("-") && args.length > i + 1) {
				_properties.put(args[i].substring(1), args[i + 1]);
				i++;
			} else {
				System.err
						.println("Warning: Parameter "
								+ args[i]
								+ " ignored because it did not follow the -[key] [value] format, thus it was discarded");
			}
		}
	}

	public void loadFromPropertyFile(String file) {
		try {
			_properties.load(new FileInputStream(file));
		} catch (FileNotFoundException e) {
			System.err.println("could not find properties file:" + file);
			e.printStackTrace();
		} catch (IOException e) {
			System.err.println("properties file: " + file + " is invalid");
			e.printStackTrace();
		}
	}

	public void setRequiredParameters(String[] parameters) {
		_required = parameters;
	}

	public String[] getMissingParamters() {
		LinkedList<String> missing = new LinkedList<String>();
		for (String req : _required) {
			if (!_properties.containsKey(req)) {
				missing.add(req);
			}
		}
		return (String[]) missing.toArray(new String[0]);
	}

	public Properties getProperties() {
		return _properties;
	}
}
